use sea_orm::{ query::*, DatabaseConnection, FromQueryResult };
use serde::{ Deserialize, Serialize };
use crate::sea_orm::sql::sql_trait::SqlIn;
#[derive(Debug, Deserialize, Serialize, FromQueryResult)]
pub struct Data {}
// 删除文件
pub async fn index<T:SqlIn>(field:&str, value:T, file_names:Option<Vec<&str>>, conn:&DatabaseConnection, table:&str, mongo:bool) -> Option<Vec<String>>{
    if let Some(file_names) = file_names {
        if file_names.is_empty() { 
            return None;
        }
        let names = file_names.join(",");

        let stmt = Statement::from_string(
            conn.get_database_backend(), 
            format!("select {} from {} where {}", names, table, value.val(field))
         );

        let data = Data::find_by_statement(stmt).into_json().one(conn).await.unwrap_or_default();
        if let Some(f) = data {
            let mut files:Vec<String> = vec![];
            for v in &file_names {
                if let Some(path) = f[v].as_str() {
                    if mongo {
                        files.push(path.to_owned());
                    }else{
                        files.push(format!("./upload/{}", path));
                    }
                } else if let Some(k) = f[v].as_array() {
                    for r in k {
                        if let Some(path) = r.as_str() {
                            if mongo {
                                files.push(path.to_owned());
                            }else{
                                files.push(format!("./upload/{}", path));
                            }
                        }
                    }
                }
            }
            // if mongo {
            //     _ = delete_files_url(files).await?;
            // }else{
            //     _ = remove_files_rel(files).await?;
            // }
            return Some(files)
        }else{
            return None
        }
    }
    None
}